package sy3;

import java.util.Random;

public class BinaryOperation {
    private int leftOperand;  // 修正：应该是 leftOperand 不是 leftOperator
    private int rightOperand; // 修正：应该是 rightOperand 不是 rightOperator
    private char operator;
    private Random random;

    /**
     * 默认构造方法
     */
    public BinaryOperation() {
        this.random = new Random();
        this.leftOperand = 0;
        this.rightOperand = 0;
        this.operator = '+';  // 修正：应该是 '+' 不是 '*'
    }
    
    /**
     * 带参数构造方法 - 修正参数列表
     */
    public BinaryOperation(int left, int right, char op) {  // 修正：参数名
        this();
        this.leftOperand = left;
        this.rightOperand = right;
        this.operator = op;
    }
    
    // 添加访问器方法
    public int getLeftOperand() {
        return leftOperand;
    }
    
    public int getRightOperand() {
        return rightOperand;
    }
    
    public char getOperator() {
        return operator;
    }
    
    // 添加 toString 方法
    @Override
    public String toString() {
        return leftOperand + "" + operator + "" + rightOperand + "=";
    }
    
    // 添加 construct 方法
    public void construct(char op) {
        this.operator = op;
        if (op == '+') {
            generateAdditionOperation();
        } else {
            generateSubstractOperation();
        }
    }
    
    // 添加生成加法算式方法
    public void generateAdditionOperation() {
        this.operator = '+';
        this.leftOperand = random.nextInt(100);
        this.rightOperand = random.nextInt(100);
    }
    
    // 添加生成减法算式方法
    public void generateSubstractOperation() {
        this.operator = '-';
        this.leftOperand = random.nextInt(100);
        // 确保减法结果非负
        this.rightOperand = random.nextInt(leftOperand + 1);
    }
    
    // 添加生成混合算式方法
    public void generateBinaryOperation() {
        if (random.nextBoolean()) {
            generateAdditionOperation();
        } else {
            generateSubstractOperation();
        }
    }
    
    // 添加 equals 方法
    public boolean equals(BinaryOperation other) {
        if (other == null) return false;
        return this.leftOperand == other.leftOperand &&
               this.rightOperand == other.rightOperand &&
               this.operator == other.operator;
    }
    
    // 添加计算结果方法
    public int calculateResult() {
        if (operator == '+') {
            return leftOperand + rightOperand;
        } else {
            return leftOperand - rightOperand;
        }
    }
}